
#ifndef __TApplet_H__
#define __TApplet_H__

#include "_TApplet.h"
#include "CContainer.h"

CarClass(TApplet)
{
    friend ECode _Impl_TApplet_Create(
                    PVoid * ppAppletObj,
                    PInterface * ppCallbackContext);
    friend ECode _Impl_TApplet_Prepare(PVoid pAppletObj);
    friend ECode _Impl_TApplet_DoWork(PVoid pAppletObj, ECode ec);
    friend ECode _Impl_TApplet_SetAppletPath(
                    PVoid pAppletObj,
                    WStringBuf* pAppletPath);

public:
    TApplet();

    virtual ~TApplet();

public:
    CARAPI Start(
        /* [in] */ const BufferOf<WString> & args);

    CARAPI Finish(
        /* [in] */ AppletFinish finishFlag);

    CARAPI GetMainThread(
        /* [out] */ IThread ** ppThread);

    CARAPI GetAllThreads(
        /* [out] */ IObjectEnumerator ** ppThreads);

    CARAPI WaitUntilFinished(
        /* [in] */ Millisecond32 timeout,
        /* [out] */ WaitResult * pResult);

    CARAPI GetStatus(
        /* [out] */ AppletStatus * pStatus);

    CARAPI SetCurrentDirectory(
        /* [in] */ WString path);

    CARAPI GetCurrentDirectory(
        /* [out] */ WStringBuf * pPath);

    CARAPI GetCurrentDirectoryInStorage(
        /* [in] */ WString storageName,
        /* [out] */ WStringBuf * pPath);

    CARAPI SetEnvironmentVariable(
        /* [in] */ WString name,
        /* [in] */ WString value);

    CARAPI GetEnvironmentVariable(
        /* [in] */ WString name,
        /* [out] */ WStringBuf * pValue);

    CARAPI SetPriorityRank(
        /* [in] */ ThreadPriorityRank priorityRank);

    CARAPI GetPriorityRank(
        /* [out] */ ThreadPriorityRank *priorityRank);

    CARAPI GetStartTime(
        /* [out] */ SystemTime *time);

    CARAPI Main(
        /* [in] */ const BufferOf<WString> & args);

    CARAPI Idle();

    CARAPI Activate();

    CARAPI Deactivate();

    CARAPI IsActivated(
        /* [out] */ Boolean *activated);

    CARAPI Snooze();

    CARAPI Wakeup();

    CARAPI GetName(
        /* [out] */ WStringBuf * name);

    CARAPI GetAllForms(
        /* [out] */ IObjectEnumerator **forms);

    CARAPI SetAppletGUI(
        /* [in] */ IAppletGUI *appletGUI);

    CARAPI NotifyActivated(
        /* [in] */ Flags32 cFlag);

    CARAPI NotifyDeactivated(
        /* [in] */ Flags32 cFlag);

    CARAPI NotifyLayoutReady(
        /* in] */ Flags32 cFlag);

    CARAPI NotifyThreadCreated(
        /* [in] */ IThread *thread);

    CARAPI NotifyThreadQuit(
        /* [in] */ IThread *thread);

    CARAPI ObjectEnter(
        /* [in] */ PInterface pObject);

    CARAPI ObjectLeave(
        /* [in] */ PInterface pObject);

    CARAPI CreateObject(
        /* [in] */ const ClassID & clsid,
        /* [in] */ const InterfaceID & iid,
        /* [out] */ PInterface * ppObject);

protected:
    virtual CARAPI OnObjectEntering(PInterface pObj) { return NOERROR; }

    virtual CARAPI OnObjectEntered(PInterface pObj) { return NOERROR; }

    virtual CARAPI OnObjectLeaving(PInterface pObj) { return NOERROR; }

    virtual CARAPI OnObjectLeft(PInterface pObj) { return NOERROR; }

private:
    static ECode AppletRoutine(PVoid pParam);

private:
    IThread*            _m_pThread;
    WStringBuf_<260>    _m_wsCurrentPath;
    DLinkNode           _m_HeadOfStorageList;
    DLinkNode           _m_HeadOfEnvVarList;

    CContainer          _m_threadContainer;
    IAppletGUI*         _m_pAppletGUI;
#ifdef _DEBUG
    ThreadId            _m_nThreadId;
#endif
};

#endif // __TApplet_H__
